﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiaoYuStock.Model.Models;
using XiaoYuStock.Model.Request;
using XiaoYuStockAnalysis.BusinessLogic.Business;
using XiaoYuStock.Core;
using System.Threading.Tasks;
using XiaoYuStock.Core.Entities;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Entities;
namespace XiaoYuStock.WebApp.Controllers
{
    public class MyTradeController : BaseController
    {

        private MyTradDataBusiness shareBus = new MyTradDataBusiness();

        private HistoryDataBusiness historyBus = new HistoryDataBusiness();
        //
        // GET: /My/

        public ActionResult Index(MyTradRequest request)
        {
            var myShares = new ShareTargetBusiness().GetList(new ShareTargetRequest() { PageIndex = 1, PageSize = 100 });
            if (!myShares.Any())
                myShares = new List<SharesInfo>();
            var t = myShares.ToList();
            t.Insert(0, new SharesInfo() { ShareCode = "0", ShareName = "不限" });
            ViewData.Add("CustomerCode", new SelectList(t, "ShareCode", "ShareName", request.CustomerCode));
            if (request.CustomerCode == "0")
                request.CustomerCode = null;
            var result = shareBus.GetList(request);
            foreach (var r in result)
            {
                var history = historyBus.GetModel(new HistoryRequest {  CurrentDate =r.TradeTime ,ShareCode=r.ShareCode});
                if (history != null)
                {
                    r.ClosePrice = history.CloseAmout;
                    r.MaxPrice = history.MaxAmount;
                    r.MinPrice = history.MinAmount;
                    r.OpenPrice = history.OpenAmount;
                }
            }
            return View(result);
        }

        public ActionResult Create()
        {
            var model = new MyTradData();
            var myShares = new ShareTargetBusiness().GetList(new ShareTargetRequest() { PageIndex = 1, PageSize = 100 });
            if (!myShares.Any())
                myShares = new List<SharesInfo>();
            var t = myShares.ToList();
            t.Insert(0, new SharesInfo() { ShareCode = "", ShareName = "不限" });
            ViewData.Add("ShareCode", new SelectList(t, "ShareCode", "ShareName",model.ShareCode));

            var sharesType = new List<KeyValuePair<int, string>>();
            foreach (string s in Enum.GetNames(typeof(TradeStatus)))
            {
                sharesType.Add(new KeyValuePair<int, string>((int)Enum.Parse(typeof(TradeStatus), s, true), s));
            }

            ViewData.Add("TradedState", new SelectList(sharesType, "Key", "Value", model.TradedState));
            model.TradeTime = DateTime.Now;
            return View("Edit", model);
        }

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {


            var model = new MyTradData();
            this.TryUpdateModel<MyTradData>(model);

            try
            {
                model.Status = 101;
                model.SynFlag = 0;
                shareBus.Save(model);
            }
            catch (BusinessException e)
            {
                this.ModelState.AddModelError(e.Name, e.Message);
                var myShares = new ShareTargetBusiness().GetList(new ShareTargetRequest() { PageIndex = 1, PageSize = 100 });
                if (!myShares.Any())
                    myShares = new List<SharesInfo>();
                var t = myShares.ToList();
                t.Insert(0, new SharesInfo() { ShareCode = "", ShareName = "不限" });
                ViewData.Add("ShareCode", new SelectList(t, "ShareCode", "ShareName", model.ShareCode));

                var sharesType = new List<KeyValuePair<int, string>>();
                foreach (string s in Enum.GetNames(typeof(TradeStatus)))
                {
                    sharesType.Add(new KeyValuePair<int, string>((int)Enum.Parse(typeof(TradeStatus), s, true), s));
                }

                ViewData.Add("TradedState", new SelectList(sharesType, "Key", "Value", model.TradedState));
                return View("Edit", model);
            }


            return this.RefreshParent();
        }
        public ActionResult Edit(int id)
        {
            var model = shareBus.Get(id);
            if (model == null)
            {
                model = new MyTradData();
            }
            var myShares = new ShareTargetBusiness().GetList(new ShareTargetRequest() { PageIndex = 1, PageSize = 100 });
            if (!myShares.Any())
                myShares = new List<SharesInfo>();
            var t = myShares.ToList();
            t.Insert(0, new SharesInfo() { ShareCode = "", ShareName = "不限" });
            ViewData.Add("ShareCode", new SelectList(t, "ShareCode", "ShareName", model.ShareCode));

            var sharesType = new List<KeyValuePair<int, string>>();
            foreach (string s in Enum.GetNames(typeof(TradeStatus)))
            {
                sharesType.Add(new KeyValuePair<int, string>((int)Enum.Parse(typeof(TradeStatus), s, true), s));
            }

            ViewData.Add("TradedState", new SelectList(sharesType, "Key", "Value", model.TradedState));
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var model = shareBus.Get(id);
            this.TryUpdateModel<MyTradData>(model);
            try
            {
                model.Status = 101;
                model.SynFlag = 0;
                shareBus.Save(model);
            }
            catch (BusinessException e)
            {
                this.ModelState.AddModelError(e.Name, e.Message);
                var myShares = new ShareTargetBusiness().GetList(new ShareTargetRequest() { PageIndex = 1, PageSize = 100 });
                if (!myShares.Any())
                    myShares = new List<SharesInfo>();
                var t = myShares.ToList();
                t.Insert(0, new SharesInfo() { ShareCode = "", ShareName = "不限" });
                ViewData.Add("ShareCode", new SelectList(t, "ShareCode", "ShareName", model.ShareCode));

                var sharesType = new List<KeyValuePair<int, string>>();
                foreach (string s in Enum.GetNames(typeof(TradeStatus)))
                {
                    sharesType.Add(new KeyValuePair<int, string>((int)Enum.Parse(typeof(TradeStatus), s, true), s));
                }

                ViewData.Add("TradedState", new SelectList(sharesType, "Key", "Value", model.TradedState));
                return View("Edit", model);
            }

            return this.RefreshParent();
        }

    }
}
